package com.ca.ui.core.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ca.ui.core.R;
import com.ca.ui.core.bean.MiniClubInfo;
import com.ca.ui.core.view.card.MiniInfoCard;

import java.util.ArrayList;
import java.util.List;

public class ClubSquareAdapter extends RecyclerView.Adapter<ClubSquareAdapter.ClubSquareHolder> {

    private List<MiniClubInfo> list;

    public ClubSquareAdapter(List<MiniClubInfo> list) {
        this.list = list == null ? new ArrayList<>() : list;
    }

    @NonNull
    @Override
    public ClubSquareHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.club_square_item_layout, parent, false);
        return new ClubSquareHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ClubSquareHolder holder, int position) {
        MiniClubInfo info = list.get(position);
        holder.infoCard.setImageUrl(info.getImageUrl());
        holder.infoCard.setName(info.getName());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setList(List<MiniClubInfo> list) {

        if (list == null) {
            list = new ArrayList<>();
        }

        if (!this.list.equals(list)) {
            this.list = list;
            notifyDataSetChanged();
        }
    }

    public static class ClubSquareHolder extends RecyclerView.ViewHolder {

        public MiniInfoCard infoCard;

        public ClubSquareHolder(@NonNull View itemView) {
            super(itemView);
            infoCard = itemView.findViewById(R.id.card_miniInfo);
        }
    }
}
